package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.StorageBox;
import com.mt.roll.core.dal.service.StorageBoxServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.StorageBoxInfoVoMapper;
import com.mt.roll.wms.params.StorageBoxInfoParam;
import com.mt.roll.wms.params.StorageBoxInfoQueryParam;
import com.mt.roll.wms.service.StorageBoxInfoService;
import com.mt.roll.wms.vo.StorageBoxCircInfoVo;
import com.mt.roll.wms.vo.StorageBoxInfoVo;
import com.mt.roll.wms.vo.StorageBoxVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2021/11/29 15:47
 */

@Service
@Transactional
public class StorageBoxInfoServiceImpl extends BaseService implements StorageBoxInfoService {

    @Autowired
    private StorageBoxServiceBiz storageBoxServiceBiz;

    @Autowired
    private StorageBoxInfoVoMapper storageBoxInfoVoMapper;

    @Override
    public R<PageVo<StorageBoxInfoVo>> page(StorageBoxInfoQueryParam storageBoxInfoQueryParam) {
        //IPage<StorageBoxInfoVo> page = storageBoxInfoVoMapper.page(new Page<>(storageBoxInfoQueryParam.getCurrent(),storageBoxInfoQueryParam.getSize()), storageBoxInfoQueryParam);
        IPage<StorageBoxInfoVo> storageBoxInfoVoIPage = new Page<>(storageBoxInfoQueryParam.getCurrent(),storageBoxInfoQueryParam.getSize());
        IPage<StorageBoxInfoVo> page = storageBoxInfoVoMapper.page(storageBoxInfoVoIPage, storageBoxInfoQueryParam);
        return successful(new PageVo<>(page,StorageBoxInfoVo.class));
    }

    @Override
    public R<StorageBoxInfoVo> get(IdParam idParam) {
        StorageBox storageBox = storageBoxServiceBiz.getById(idParam.getId());
        Assert.notNull("任务详情不存在", storageBox);
        StorageBoxInfoVo storageBoxInfoVo = storageBoxInfoVoMapper.getById(idParam.getId());
        return successful(storageBoxInfoVo);
    }

    @Override
    public R<IdVo> add(StorageBoxInfoParam storageBoxInfoParam) {
        QueryWrapper<StorageBox> wrapper = new QueryWrapper<>();
        wrapper.eq(StorageBox.CODE, storageBoxInfoParam.getCode());
        Assert.eqZero(storageBoxServiceBiz.count(wrapper), "该存储箱已经存在，请勿重复添加");
        StorageBox storageBox = new StorageBox();
        BeanUtils.copyProperties(storageBoxInfoParam, storageBox);
        setCommonField(storageBox);
        storageBoxServiceBiz.save(storageBox);
        return successful(IdVo.builder().id(storageBox.getId()).build());
    }

    @Override
    public R<IdVo> update(StorageBoxInfoParam storageBoxInfoParam) {
        StorageBox storageBox = storageBoxServiceBiz.getById(storageBoxInfoParam.getId());
        /*修改信息需要检验是否已存在
        if (!storageBox.getCode().equals(storageBoxParam.getCode())) {
            QueryWrapper<StorageBox> wrapper = new QueryWrapper<>();
            wrapper.eq(StorageBox.CODE, storageBoxParam.getCode());
            Assert.eqZero(storageBoxServiceBiz.count(wrapper), "该信息已经存在");
        }*/
        StorageBox updateStorageBox = new StorageBox();
        BeanUtils.copyProperties(storageBoxInfoParam, updateStorageBox);
        setUpdateCommonField(updateStorageBox);
        storageBoxServiceBiz.updateById(updateStorageBox);
        return successful(IdVo.builder().id(storageBox.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        StorageBox storageBox = storageBoxServiceBiz.getById(idParam.getId());
        if (storageBox == null) {
            return failed("存储箱不存在");
        }
        /*
        StorageBox updateAlarm = new StorageBox().setId(storageBox.getId());
        updateAlarm.setValid(WhetherEnum.NO.getValue());
        //setUpdateCommonField(updateAlarm);
        storageBoxServiceBiz.updateById(updateAlarm);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        storageBoxServiceBiz.removeById(updateAlarm.getId());
        */
        storageBoxServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(storageBox.getId()).build());
    }

    @Override
    public R<String> getCode() {
        return successful(CodeGeneratorHelper.getStorageBoxCode());
    }
}
